<template>
  <div>
    <div class="container" v-if="schemeInfo.shpSchemeKind&& schemeInfo.shpSchemeKind.length>0">
      <div class="section">
        <div class="section-content">
          <!--  list 修改区域 -->
          <div v-for="(table, i) in schemeInfo.shpSchemeKind" :key="i">
            <label class="section-title">{{ table.KindName }}</label>
            <!-- <a-list bordered :data-source="table.shpSchemeDetailed" class="DList"> -->

            <div bordered class="DList">
              <div slot="header">
                <div class="header">
                  <a-checkbox @change="checkAll(table,i)" :checked="table.check" class="checkBox"></a-checkbox>
                  <span>ProductName</span>
                  <span>ProductParameter</span>
                  <span>Quantity</span>
                  <span>Unit</span>
                  <span>Imgs</span>
                  <span>Price</span>
                </div>
              </div>
              <a-list-item
                slot="renderItem"
                v-for="(item, index) in table.shpSchemeDetailed"
                :key="index"
              >
                <div class="line">
                  <a-checkbox
                    @change="checkLine(item,i,index)"
                    class="checkBox"
                    :checked="item.check"
                  ></a-checkbox>
                  <span>{{ item.ProductName }}</span>
                  <span>{{ item.ProductParameter }}</span>
                  <span>
                    <a-input-number
                      v-model="item.Quantity"
                      :min="0"
                      :max="10000"
                      @change="onChangeQuantity(item,i)"
                    />
                  </span>
                  <span>{{ item.Unit }}</span>
                  <span>
                    <viewer style="width: 150px;height: 150px; overflow: hidden;">
                      <img
                        v-if="item.ImageUrl"
                        :src="item.ImageUrl"
                        style="width:150px;height:150px;"
                        @error="errorImg"
                      />
                      <img style="width:150px;height:150px;" v-else src="@/assets/img/noImg.png" />
                    </viewer>
                  </span>
                  <span>{{ item.Price }}</span>
                </div>
              </a-list-item>
            </div>
            <!-- 小计区域 -->
            <div class="totalPrice sub_total">
              <div class="price-item">
                <div class="price-name">
                  <span>subTotal：</span>
                  <span class="price-trade">
                    <span class="ng-star-inserted">
                      <span class="price-cny-symbol">$</span>
                      <span class="price-money">{{ sub_totalList[Number(i)] }}</span>
                      <span class="price-cny-unit"></span>
                      <span class="price-count-unit"></span>
                    </span>
                  </span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="totalPrice sub_total">
        <div class="price-item">
          <div class="price-name">
            <span>Total：</span>
            <span class="price-trade">
              <span class="ng-star-inserted">
                <span class="price-cny-symbol">$</span>
                <span class="price-money">{{ totalAmount }}</span>
                <span class="price-cny-unit"></span>
                <span class="price-count-unit"></span>
              </span>
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      schemeInfo: [],
      totalAmount: 0,
      sub_totalList: [],
    }
  },
  mounted() {
    this.getSchemeDetail()
  },
  methods: {
    onChangeQuantity(text, index) {
      const that = this
      let i = Number(index)
      console.log(text, 'text', index, 'index')
      this.$nextTick(() => {
        that.schemeInfo.shpSchemeKind[i].sub_total = 0
        that.sub_totalList[i] = 0
        console.log(that.schemeInfo.shpSchemeKind[i].shpSchemeDetailed)
        that.schemeInfo.shpSchemeKind[i].shpSchemeDetailed.map((v, j) => {
          if (v.check == true) {
            that.schemeInfo.shpSchemeKind[i].sub_total += v.Price * v.Quantity
            that.sub_totalList[i] += v.Price * v.Quantity
            this.$forceUpdate()
          }
        })
        console.log('v :>> ', that.sub_totalList[i])
        that.totalAmount = that.sub_totalList.reduce((x, y) => {
          return Number(x) + Number(y)
        })
      })

      // this.reMathInfo()
    },
    // 方案的详细信息
    getSchemeDetail() {
      const that = this
      const a = {
        method: 'ShpSchemeInfo_Que',
        userId: window.userId || 1,
        sign: '1',
        currentPage: 0,
        limit: 0,
        language: window.language || 1,
        datas: {
          SchemeId: this.theSchemeId,
        },
      }
      this.$post(a).then((result) => {
        this.schemeInfo = result.data
        this.$hideLoading()
        console.log('this.schemeInfo :>> ', this.schemeInfo)
        this.schemeInfo.CreateTime = this.schemeInfo.CreateTime
          ? this.schemeInfo.CreateTime.replace('T', ' ').substr(0, 10)
          : ''
        this.schemeInfo.DueDate = this.schemeInfo.DueDate
          ? this.schemeInfo.DueDate.replace('T', ' ').substr(0, 10)
          : ''
        this.Summary = this.schemeInfo.Summary ? this.schemeInfo.Summary : ''
        this.imageArr = this.schemeInfo.SchemeImage
          ? eval('(' + this.schemeInfo.SchemeImage + ')')
          : []

        this.DetailedInformation = this.schemeInfo.Introduction
          ? this.schemeInfo.Introduction.split('\\').join('')
          : ''
        this.schemeInfo.shpSchemeRecommend.map((item) => {
          item.SchemeImage = item.SchemeImage
            ? eval('(' + item.SchemeImage + ')')
            : []
          return item
        })
        this.schemeInfo.shpSchemeSME.map((item) => {
          item.SchemeImage = item.SchemeImage
            ? eval('(' + item.SchemeImage + ')')
            : []
          return item
        })
        this.$nextTick(() => {
          that.totalAmount = 0
          this.schemeInfo.shpSchemeKind.map((item, index) => {
            item.sub_total = 0
            item.check = true
            this.sub_totalList[index] = 0
            if (item.shpSchemeDetailed && item.shpSchemeDetailed.length > 0) {
              item.shpSchemeDetailed.map((v, i) => {
                that.sub_totalList[index] += v.Price * v.Quantity
                v.check = true
                return v
              })
            }
            that.totalAmount = that.sub_totalList.reduce((x, y) => {
              return Number(x) + Number(y)
            })
            return item
          })
         
        })
      })
    },
  },
}
</script>

<style lang="less" scoped>
.container {
  font-size: 12px;
  font-family: PingFangSC-Regular;
  line-height: 18px;
  min-height: 380px;
  color: #373d41;
  padding: 20px;
  position: relative;
  // background-color: #f5f5f6;

  .section-title {
    width: 112px;
    font-size: 25px;
    line-height: 24px;
    font-family: 'Helvetica Neue For Number', -apple-system, BlinkMacSystemFont,
      'Segoe UI', Roboto, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei',
      'Helvetica Neue', Helvetica, Arial, sans-serif !important;
    box-sizing: border-box;
    font-weight: 450;
    padding: 28px 0;
    display: block;
  }

  .section-content {
    width: 100%;
    margin-top: 58.8px;
  }

  .sub_total {
    .price-item {
      display: flex;
      flex-direction: column;
      align-items: flex-end;
      justify-content: center;
      padding: 0 20px;
      line-height: 1.3;
    }

    .price-name {
      color: #373d41;
      font-size: 12px;
    }

    .price-trade {
      color: #ff8a00;
      font-size: 12px;
    }

    .price-money {
      font-size: 24px;
      font-family: 'Helvetica Neue For Number', -apple-system,
        BlinkMacSystemFont, 'Segoe UI', Roboto, 'PingFang SC',
        'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica,
        Arial, sans-serif !important;
    }

    .price-count-unit {
      font-size: 12px;
      color: #000;
    }
  }

  .totalPrice {
    display: block;
    background: #fff;
    border-top: 1px solid #ebecec;
    box-shadow: 0 -15px 35px 0 rgba(0, 0, 0, 0.05);
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding: 20px 30px;
    margin: 10px 0;
  }
}

.DList {
  span {
    padding: 16px 16px;
    overflow-wrap: break-word;

    color: rgba(0, 0, 0, 0.85);
    text-align: center;
    flex: 1;
    display: block;
  }
  .checkBox {
    padding: 16px 16px;
    overflow-wrap: break-word;
  }
  .header {
    display: flex;
    font-weight: 600;
    align-items: center;
    background: #fafafa;
    border-bottom: 1px solid #e8e8e8;
    transition: background 0.3s ease;
  }
  .ant-list-item {
    .line {
      display: flex;
      width: 100%;
      height: 100%;
      align-items: center;
      border-bottom: 1px solid #e8e8e8;
      transition: all 0.3s, border 0s;
    }
  }
}
.sub_total {
  .price-item {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: center;
    padding: 0 20px;
    line-height: 1.3;
  }

  .price-name {
    color: #373d41;
    font-size: 12px;
  }

  .price-trade {
    color: #ff8a00;
    font-size: 12px;
  }

  .price-money {
    font-size: 24px;
    font-family: 'Helvetica Neue For Number', -apple-system, BlinkMacSystemFont,
      'Segoe UI', Roboto, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei',
      'Helvetica Neue', Helvetica, Arial, sans-serif !important;
  }

  .price-count-unit {
    font-size: 12px;
    color: #000;
  }
}

.totalPrice {
  display: block;
  background: #fff;
  border-top: 1px solid #ebecec;
  box-shadow: 0 -15px 35px 0 rgba(0, 0, 0, 0.05);
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 20px 30px;
  margin: 10px 0;
}
</style>